<?php

/**
 * Class UserControlller
 *
 * Description for class UserControlller
 *
 * @author: langxangvn
*/
class UserController extends Zend_Controller_Action {
	/**
	 * Initialize object
	 *
	 * @return void
	 */
	public function init() {
		
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function indexAction() {
		$this->view->title = "Registered users";
		$this->view->headTitle($this->view->title, 'API Server');
		$this->view->headTitle($this->view->title, 'API Server');
		try {
			$users = new Model_DbTable_Users();
			$this->view->users = $users->fetchAll();			
		} catch(Exception $e) {
			
		}		
	}
	
	/**
	 * This is method loginAction
	 *
	 * @return void
	 *
	 */
	public function loginAction() {	
		if(Zend_Auth::getInstance()->hasIdentity()) {	// already logged in			
			$this->_redirect('/');	// redirect to index
		}
		
		$this->view->title = "Please login to register app or authorize";
		$this->view->headTitle($this->view->title);
		
		$form = new Form_Login();
		$form->setAction($this->view->baseUrl('/') . '/user/login')
			->setMethod('POST');
		
		if ($this->getRequest()->isPost()) {
			$data = $this->_request->getPost();		
			if ($form->isValid($data)) {							
				$user = new Model_DbTable_Users();				// try login	
				$email = $form->getValue('auth_email');
				$password = $form->getValue('auth_password');
				
				// Check login result
				if (true === $message = $user->login($email, $password)) {					
					$this->_redirect('/');	// redirect to index
				} else {					
					$this->view->errors = $message;	// set errors
				}
			} else {
				$form->populate($data);					
			}
		}
		
		$this->view->form = $form;
	}
	
	/**
	 * This is method logoutAction
	 *
	 * @return void
	 *
	 */
	public function logoutAction() {
		// Shutdown view script and layout
		$this->_helper->viewRenderer->setNoRender(true);
		Zend_Layout::getMvcInstance()->disableLayout();
		
		$auth = Zend_Auth::getInstance();	
		// Delete the information from the session
		$auth->clearIdentity();		
		Zend_Session::forgetMe();
		
		// Redirect to home
		$this->_redirect(null);
	}	
	
	/**
	 * This is method signupAction
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function signupAction() {
		$this->view->title = "Register new user";
		$this->view->headTitle($this->view->title, 'API Server');
		
		$form = new Form_User();
		$form->submit->setLabel('Signup');
		$this->view->form = $form;
		
		if ($this->getRequest()->isPost()) {
			$data = $this->_request->getPost();
			if ($form->isValid($data)) {
				$email = $form->getValue('email');
				$fullname = $form->getValue('fullname');
				$password = $form->getValue('password');
				
				$users = new Model_DbTable_Users();
				$regs->addUser($fullname, $email, $password);
				
				$this->_redirect('/');
			} else {
				$form->populate($data);
			}
		}		
	}	
	
	/**
	 * This is method deleteAction
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function deleteAction() {
		$this->view->title = "Delete user";
		$this->view->headTitle($this->view->title, 'API Server');
		
		if ($this->getRequest()->isPost()) {
			$del = $this->getRequest()->getPost('del');
			if ($del == 'Yes') { 
				$id = $this->getRequest()->getPost('id');
				$users = new Model_DbTable_Users();
				$users->deleteUser($id);
			}
			$this->_redirect('/');
		} else {
			$id = $this->_getParam('id', 0);
			$users = new Model_DbTable_Registries();
			$this->view->user = $users->getUser($id);
		}
	}
	
	/**
	 * This is method editAction
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function editAction() {
		$this->view->title = "Edit user information";
		$this->view->headTitle($this->view->title, 'API Server');
		
		$form = new Form_User();
		$form->submit->setLabel('Save');
		$this->view->form = $form;
		
		if ($this->getRequest()->isPost()) {
			$data = $this->getRequest()->getPost();
			if ($form->isValid($data)) {
				$id = (int)$form->getValue('id');
				
				$email = $form->getValue('email');
				$fullname = $form->getValue('fullname');
				$password = $form->getValue('password');
				
				$users = new Model_DbTable_Users();
				$users->updateUser($id, $fullname, $email, $password);
				
				$this->_redirect('/');
			} else {
				$form->populate($data);
			}
		} else {
			$id = $this->_request->getParam('id', 0);
			if ($id > 0) {
				$users = new Model_DbTable_Users();
				$form->populate($users->getUser($id));
			}
		}   
	}	
}

?>